package DAL;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;

import oracle.jdbc.OraclePreparedStatement;
import DAL.LectureBD.Role;

public class FilmDAL extends Dal {

     public void insertionFilm(int id, String titre, int annee, ArrayList<String> pays, String langue, int duree, String resume, ArrayList<String> genres, String realisateurNom, int realisateurId,
               ArrayList<String> scenaristes, ArrayList<Role> roles, String poster, ArrayList<String> annonces) {

          CallableStatement pstmt = null;
          try {

               pstmt = Dal.connection.prepareCall("{call sproc_ajouterFilm(?,?,?,?,?,?,?)}");
               pstmt.setInt(1, id);
               pstmt.setString(2, titre);
               pstmt.setInt(3, annee);
               pstmt.setString(4, langue);
               pstmt.setInt(5, duree);
               ((OraclePreparedStatement) pstmt).setStringForClob(6, resume);
               pstmt.setString(7, poster);

               pstmt.registerOutParameter(1, Types.INTEGER);
               pstmt.executeUpdate();

               int filmId = pstmt.getInt(1);
               System.out.println("movie:" + filmId +" inserted");
               CallableStatement pstmt2 = null;
               for (int i = 0; i < annonces.size(); i++) {
                    pstmt2 = Dal.connection.prepareCall("{call sproc_ajouterFilmAnnonces(?,?)}");
                    pstmt2.setInt("var_id_film", filmId);
                    pstmt2.setString("var_annonce", annonces.get(i));
                    pstmt2.executeUpdate();
                    pstmt2.close();
               }

               CallableStatement pstmt3 = null;
               for (int i = 0; i < genres.size(); i++) {
                    pstmt3 = Dal.connection.prepareCall("{call sproc_ajouterFilmGenre(?,?)}");
                    pstmt3.setInt("var_id_film", filmId);
                    pstmt3.setString("var_film_genre", genres.get(i));
                    pstmt3.executeUpdate();
                    pstmt3.close();
               }

               CallableStatement pstmt4 = null;
               for (int i = 0; i < pays.size(); i++) {
                    pstmt4 = Dal.connection.prepareCall("{call sproc_ajouterFilmPays(?,?)}");
                    pstmt4.setInt("var_id_film", filmId);
                    pstmt4.setString("var_film_pays", pays.get(i));
                    pstmt4.executeUpdate();
                    pstmt4.close();
               }
               System.out.println("var_id_realisateur" + realisateurId);
               if (realisateurId != -1) {
                    CallableStatement pstmt5 = null;
                    pstmt5 = Dal.connection.prepareCall("{call sproc_ajouterFilmRealisateur(?,?,?,?)}");
                    pstmt5.setInt("var_id_film", filmId);
                    pstmt5.setInt("var_id_realisateur", realisateurId);
                    String[] nomPrenom = retrievePrenomNom(realisateurNom);
                    pstmt5.setString("var_prenom_realisateur", nomPrenom[0]);
                    if (nomPrenom.length > 1) {
                         pstmt5.setString("var_nom_realisateur", nomPrenom[1]);
                    } else {
                         pstmt5.setString("var_nom_realisateur", nomPrenom[0]);
                    }
                    pstmt5.executeUpdate();
                    pstmt5.close();
               }
               
               CallableStatement pstmt6 = null;
               for (int i = 0; i < roles.size(); i++) {
                    pstmt6 = Dal.connection.prepareCall("{call sproc_ajouterFilmRoleActeur(?,?,?,?,?)}");
                    pstmt6.setInt("var_id_film", filmId);
                    pstmt6.setInt("var_id_acteur", roles.get(i).id);
                    
                    pstmt6.setString("var_prenom_acteur", roles.get(i).nom.split(" ")[0]);
                    if (roles.get(i).nom.split(" ").length > 1) {
                         pstmt6.setString("var_nom_acteur", roles.get(i).nom.split(" ")[1]);
                         System.out.println("nom:" + roles.get(i).nom.split(" ")[0] + " " +roles.get(i).nom.split(" ")[1]  );
                    } else {
                         pstmt6.setString("var_nom_acteur", roles.get(i).nom.split(" ")[0]);
                         System.out.println("nom:" + roles.get(i).nom.split(" ")[0] + " " +roles.get(i).nom.split(" ")[0]  );
                    }
                    pstmt6.setString("var_personnage_acteur", roles.get(i).personnage);
                    pstmt6.executeUpdate();
                    pstmt6.close();
               }
               
               
               CallableStatement pstmt7 = null;
               for (int i = 0; i < scenaristes.size(); i++) {
                    pstmt7 = Dal.connection.prepareCall("{call sproc_ajouterFilmScenariste(?,?,?)}");
                    pstmt7.setInt("var_id_film", filmId);
                    pstmt7.setString("var_prenom_scenariste", scenaristes.get(i).split(" ")[0]);
                    if (scenaristes.get(i).split(" ").length > 1) {
                         pstmt7.setString("var_nom_scenariste", scenaristes.get(i).split(" ")[1]);
                    }else
                         pstmt7.setString("var_nom_scenariste", scenaristes.get(i).split(" ")[0]);
                    pstmt7.executeUpdate();
                    pstmt7.close();
               }
               

          } catch (SQLException e) {

               e.printStackTrace();
          } finally {
               try {
                    pstmt.close();
               } catch (SQLException e) {
                    System.out.println("HAPPEN ON THE CLOSE");
                    e.printStackTrace();
               }
          }

     }
     
     private String[] retrievePrenomNom(String arg){ 
	        String[] result = {"", ""}; 
	          
	        if(arg != null && arg.length() > 0){ 
	            String[] tmp = arg.split(" "); 
	            int tmpLen = tmp.length; 
	            if(tmpLen > 1){ 
	                String prenom = ""; 
	                for(int i = 0; i < (tmpLen -1); i++){ 
	                    prenom += tmp[i]; 
	                } 
	                String nom = tmp[tmpLen -1]; 
	                  
	                result[0] = prenom; 
	                result[1] = nom; 
	            }
	            else{
	            	result[0]= arg;
	            	result[1]= arg;
	            }
	        } 
	          
	        return result; 
	    } 
}
